<template>
  <div class></div>
</template>
<script>
import axios from "@/utils/requestdp";

export default {
  name: "APIqueryDbPipenet",
  components: {},
  data() {
    return {};
  },
  computed: {
    ishistory: function() {
      return this.$store.state.daping.ishistorytime;
    },
    timerangarr: function() {
      return this.$store.state.daping.timepick;
    }
  },
  watch: {},
  mounted() {
    this.watchEvent();
  },
  methods: {
    watchEvent() {
      //先清除定时任务
      if (
        this.$store.state.daping.intervalArr["APIqueryDbPipenet"] != undefined
      ) {
        clearInterval(
          this.$store.state.daping.intervalArr["APIqueryDbPipenet"][0].interval
        );
      }
      this.loadData();
      let interval = setInterval(() => {
        this.loadData();
      }, this.$root.setting.data.interval);
      this.$store.state.daping.intervalArr["APIqueryDbPipenet"] = [
        { interval: interval }
      ];
    },
    loadData() {
      let queryJson = {
        queries: [
          {
            metric: "*",
            schema: "statistic"
          }
        ],
        table: "pipenet"
      };

      if (this.ishistory == "历史") {
        queryJson = {
          queries: [
            {
              metric: "*",
              schema: "statistic"
            }
          ],
          table: "pipenet",
          type: "history",
          startTime: this.timerangarr[0].getTime(),
          endTime: this.timerangarr[1].getTime()
        };
      }

      axios.post("/open/queryDb", queryJson).then(res => {
        let pipenetDataObj = {};
        if (res.data.length > 0) {
          var pipenetData = res.data;
          pipenetData.map(item => {
            if (
              pipenetDataObj[item["col_name"] + "@" + item["id"]] == undefined
            ) {
              pipenetDataObj[item["col_name"] + "@" + item["id"]] =
                item["value"];
            }
            if (
              pipenetDataObj["aligntime"] == undefined ||
              pipenetDataObj["aligntime"] <= Date.parse(item["aligntime"])
            ) {
              pipenetDataObj["aligntime"] = Date.parse(item["aligntime"]);
            }
          });
        } else {
          //如果没有则提示错误信息 置空对象
        }
        this.$store.state.daping.queryDbPipenet = pipenetDataObj;
      });
    }
  }
};
</script>
<style lang='scss' scoped>
</style>